﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LiveFX.Client;
using Microsoft.LiveFX.ResourceModel;
using System.Windows.Forms;

namespace LiveFrameworkExplorer.Plugins.LiveFxBrowser
{
    public class LiveFxBrowserPlugin : IPlugin, IPluginItem
    {
        private ILFEBase _lfeBase = null;

        #region IPlugin Members

        public void Init(ILFEBase lfeBase)
        {
            _lfeBase = lfeBase;
        }

        public List<Type> GetTypes()
        {
            return new List<Type>() { 
                typeof(object), 
            };
        }

        public string GetMenuItemLabel()
        {
            return "View in the browser";
        }

        public System.Drawing.Image GetMenuItemImage()
        {
            return LiveFrameworkExplorer.Properties.Resources.searchWeb;
        }

        public bool CheckItem<TMeshItem, TResource>(object item)
            where TMeshItem : Microsoft.LiveFX.Client.LiveItem<TResource>, new()
            where TResource : Microsoft.LiveFX.ResourceModel.Resource, new()
        {
            return true;
        }

        public void OpenPlugin<TMeshItem, TResource>(object item, object parentItem)
            where TMeshItem : Microsoft.LiveFX.Client.LiveItem<TResource>, new()
            where TResource : Microsoft.LiveFX.ResourceModel.Resource, new()
        {
            if (item is TMeshItem)
                OpenItemURI((item as TMeshItem).Resource.SelfLink);
            else if (item is LiveItemCollection<TMeshItem, TResource>)
                OpenItemURI((item as LiveItemCollection<TMeshItem, TResource>).SelfLink);
            else
            {
                _lfeBase.DisplayException(this, new Exception("Error to read the item " + item.GetType()));
            }
        }

        #endregion

        private void OpenItemURI(Uri itemUri)
        {
            object tmpObject = null;
            var window = _lfeBase.CreateWindowToolbox(this, typeof(ucBrowser).FullName, "Live Mesh Browser", ref tmpObject);
            (tmpObject as ucBrowser).SetParameters(_lfeBase, itemUri);
            window.Linkable = false;
            window.IsFloating = false;
            window.Visible = true;
        }
    }
}
